package com.mxpio.erp.plm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mxpio.erp.common.enums.PlmEnums;
import com.mxpio.erp.common.enums.ProjectTaskEnums;
import com.mxpio.erp.common.plm.entity.*;
import com.mxpio.erp.common.plm.service.*;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目列表模板
 */
@Service
public class ProjectInfoTemplateServiceImpl extends BaseServiceImpl<ProjectInfoTemplate> implements ProjectInfoTemplateService {
    @Autowired
    private ProjectInfoService projectInfoService;

    @Autowired
    private ProjectDeliverableService projectDeliverableService;

    @Autowired
    private ProjectDeliverableTemplateService projectDeliverableTemplateService;

    @Autowired
    private ProjectTaskService projectTaskService;

    @Autowired
    private ProjectTaskTemplateService projectTaskTemplateService;

    @Autowired
    private ProjectTaskDeliverableService projectTaskDeliverableService;

    @Autowired
    private ProjectInfoTemplateService projectInfoTemplateService;

    @Autowired
    private ProjectTaskDeliverableTemplateService projectTaskDeliverableTemplateService;
    /**
     * 保存项目模板
     * @param proCode
     */
    @Override
    @Transactional
    public Result<?> saveTemplate(String proCode) {
        ProjectInfo info = projectInfoService.getById(ProjectInfo.class, proCode);
        if (info == null){
//            throw new MBootException("项目不存");
            return Result.error("项目不存在");
        }
//        ProjectInfoTemplate one = JpaUtil.linq(ProjectInfoTemplate.class).equal("proCode", proCode).findOne();
//        if (one !=null){
//            return Result.error("项目模板已存在");
//        }
        //判断模版名称是否有重复的，重复进行提醒，且生成模版不成功
        List<ProjectInfoTemplate> allInfoTemplate = this.list(ProjectInfoTemplate.class, Criteria.create());
        for (ProjectInfoTemplate projectInfoTemplate : allInfoTemplate){
            if (info.getProName().equals(projectInfoTemplate.getProName())){
                return Result.error("项目模板名称重复");
            }
        }
        if ( !info.getProStatus().equals(PlmEnums.ProStatus.EXECUT.getCode())
                && !info.getProStatus().equals(PlmEnums.ProStatus.PAUSE.getCode())
                && !info.getProStatus().equals(PlmEnums.ProStatus.FINISH.getCode())
                && !info.getProStatus().equals(PlmEnums.ProStatus.END.getCode())
                && !info.getProStatus().equals(PlmEnums.ProStatus.NEW.getCode())){
//            throw new MBootException("项目状态不正确");
            return Result.error("项目状态不正确");
        }

        //添加项目信息模板
        ProjectInfoTemplate projectInfoTemplate = new ProjectInfoTemplate();
        projectInfoTemplate.setInfoDeptId(info.getInfoDeptId());
        projectInfoTemplate.setProCode(info.getProCode());
        projectInfoTemplate.setProName(info.getProName());
        projectInfoTemplate.setPriority(info.getPriority());
        projectInfoTemplate.setProSource(info.getProSource());
        projectInfoTemplate.setProType(info.getProType());
        projectInfoTemplate.setProLeader(info.getProLeader());
        projectInfoTemplate.setProSecretary(info.getProSecretary());
        projectInfoTemplate.setPlanStartTime(info.getPlanStartTime());
        projectInfoTemplate.setPlanEndTime(info.getPlanEndTime());
        projectInfoTemplate.setDuration(info.getDuration());
        projectInfoTemplate.setProBudget(info.getProBudget());
        projectInfoTemplate.setProRange(info.getProRange());
        projectInfoTemplate.setProTarget(info.getProTarget());
        projectInfoTemplate.setRules(info.getRules());
        this.save(projectInfoTemplate);

        //添加项目交付物模板
        Criteria criteria = Criteria.create().addCriterion("proCode", Operator.EQ, proCode);
        List<ProjectDeliverable> list = projectDeliverableService.list(ProjectDeliverable.class, criteria);
//        if (list !=null && list.size() > 0){}
             for (ProjectDeliverable projectDeliverable : list){
                 ProjectDeliverableTemplate projectDeliverableTemplate = new ProjectDeliverableTemplate();
//               BeanUtil.copyProperties(projectDeliverable,projectDeliverableTemplate);
                 BeanUtil.copyProperties(projectDeliverable,projectDeliverableTemplate,"docNO");
                 projectDeliverableTemplate.setTemplateCode(projectInfoTemplate.getTemplateCode());
                 projectDeliverableTemplateService.save(projectDeliverableTemplate);
             }

        //添加项目任务
        List<ProjectTask> tasks = projectTaskService.list(ProjectTask.class, criteria);
        if (tasks !=null && tasks.size() > 0){
            for (ProjectTask projectTask : tasks){
                if (projectTask.getTaskType().equals("10")){
                    ProjectTaskTemplate projectTaskTemplate = new ProjectTaskTemplate();
                    BeanUtil.copyProperties(projectTask,projectTaskTemplate,"taskCode");
                    projectTaskTemplate.setTemplateCode(projectInfoTemplate.getTemplateCode());
                    projectTaskTemplateService.save(projectTaskTemplate);

                    //添加项目任务交付物
                    String taskCode = projectTask.getTaskCode();
                    Criteria c = Criteria.create().addCriterion("taskCode", Operator.EQ, taskCode);
                    List<ProjectTaskDeliverable> list1 = projectTaskDeliverableService.list(ProjectTaskDeliverable.class, c);
                    if (list1 !=null && list1.size() > 0){
                        for (ProjectTaskDeliverable projectTaskDeliverable : list1){
                            ProjectTaskDeliverableTemplate projectTaskDeliverableTemplate = new ProjectTaskDeliverableTemplate();
                            BeanUtil.copyProperties(projectTaskDeliverable,projectTaskDeliverableTemplate,"docNO","taskCode");
                            projectTaskDeliverableTemplate.setTemplateCode(projectInfoTemplate.getTemplateCode());
                            projectTaskDeliverableTemplate.setTaskCode(projectTaskTemplate.getTaskCode());
                            projectTaskDeliverableTemplateService.save(projectTaskDeliverableTemplate);
                        }
                    }

                }
            }
        }
        return Result.OK("项目模板新增成功");

    }

    @Override
    @Transactional
    public void deleteAll(String templateCode) {
        //删除项目模板
        this.delete(ProjectInfoTemplate.class,templateCode);
        //删除项目模板交付物
        List<ProjectDeliverableTemplate> deliverableTemplate = projectDeliverableTemplateService.list(ProjectDeliverableTemplate.class, Criteria.create().addCriterion("templateCode", Operator.EQ, templateCode));
        for (ProjectDeliverableTemplate projectDeliverableTemplate : deliverableTemplate){
            projectDeliverableTemplateService.delete(ProjectDeliverableTemplate.class,projectDeliverableTemplate.getDocNO());
        }
        //删除项目任务模板
        List<ProjectTaskTemplate> taskTemplate = projectTaskTemplateService.list(ProjectTaskTemplate.class, Criteria.create().addCriterion("templateCode", Operator.EQ, templateCode));
        for (ProjectTaskTemplate projectTaskTemplate : taskTemplate){
            projectTaskTemplateService.delete(ProjectTaskTemplate.class,projectTaskTemplate.getTaskCode());
        }
        //删除项目任务交付物
        List<ProjectTaskDeliverableTemplate> taskDeliverableTemplate = projectTaskDeliverableTemplateService.list(ProjectTaskDeliverableTemplate.class, Criteria.create().addCriterion("templateCode", Operator.EQ, templateCode));
        for (ProjectTaskDeliverableTemplate projectTaskDeliverableTemplate : taskDeliverableTemplate){
            projectTaskDeliverableTemplateService.delete(ProjectTaskDeliverableTemplate.class,projectTaskDeliverableTemplate.getDocNO());
        }

    }

}

